/*
 *  Copyright (C) 2010 Glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.commons;

import java.io.Serializable;
import javax.swing.JPanel;
import org.balisunrise.common.EntityPanel;
import org.balisunrise.common.Messenger;
import org.balisunrise.common.EntityActions;
import org.balisunrise.common.VisualEntityType;
import org.balisunrise.common.abstracts.AbstractVisualEntity;
import org.balisunrise.swing.components.panel.EditorPanel;

/**
 *
 * @author Glauco
 */
public class Editor extends AbstractVisualEntity
        implements org.balisunrise.common.Editor
        <EntityPanel<JPanel>,
         Messenger<JPanel>,
         EntityActions<?,JPanel>,
         JPanel>{

    public static final String PROP_ENTITY_PANEL = "entityPanel";
    public static final String PROP_MESSENGER = "messenger";
    public static final String PROP_ENTITY_ACTION = "entityActions";
    public static final String PROP_ENTITY = "entity";
    public static final String PROP_VISUAL_REPRESENTATION =
            "visualRepresentation";

    EntityPanel entityPanel;
    Messenger messenger;
    EntityActions entityActions;
    Serializable entity;
    EditorPanel visualRepresentation;

    public void setVisualRepresentation(EditorPanel visualRepresentation) {
        EditorPanel old = this.visualRepresentation;
        this.visualRepresentation = visualRepresentation;
        changeSupport.firePropertyChange(PROP_VISUAL_REPRESENTATION, old,
                visualRepresentation);
    }

    @Override
    public void setEntityPanel(EntityPanel entityPanel) {
        EntityPanel old = this.entityPanel;
        this.entityPanel = entityPanel;
        changeSupport.firePropertyChange(PROP_ENTITY_PANEL, old, entityPanel);
    }

    @Override
    public EntityPanel<JPanel> getEntityPanel() {
        return entityPanel;
    }

    @Override
    public void setMessenger(Messenger messenger) {
        Messenger old = this.messenger;
        this.messenger = messenger;
        changeSupport.firePropertyChange(PROP_MESSENGER, old, messenger);
    }

    @Override
    public Messenger<JPanel> getMessenger() {
        return messenger;
    }

    @Override
    public void setEntityActions(EntityActions actions) {
        EntityActions old = this.entityActions;
        this.entityActions = actions;
        changeSupport.firePropertyChange(PROP_ENTITY_ACTION, old,
                entityActions);
    }

    @Override
    public EntityActions<?,JPanel> getEntityActions() {
        return entityActions;
    }

    @Override
    public void setEntity(Serializable entity) {

        Serializable old = this.entity;

        // remove o bind
        getEntityPanel().unBind();

        // primeiro seta uma entidade nula
        getEntityPanel().setEntity(null);

        for (org.balisunrise.common.FieldLayout fl :
                getEntityPanel().getFieldLayouts()) {
            fl.clearComponentsValues();
        }

        // por fim, seta a entidade.
        getEntityPanel().setEntity(entity);

        // faz o bind
        getEntityPanel().bind();

        this.entity = entity;
        changeSupport.firePropertyChange(PROP_ENTITY, old, entity);
    }

    @Override
    public Serializable getEntity() {
        return getEntityPanel().getEntity();
    }

    @Override
    public JPanel getVisualRepresentation() {
        return visualRepresentation;
    }

    /**
     * Retorna o tipo do VisualEntity.<BR>
     * Sempre retornará EDITOR.
     * @return VisualEntityType.EDITOR
     */
    @Override
    public VisualEntityType getVisualEntityType() {
        return VisualEntityType.EDITOR;
    }
}
